#!/usr/bin/python
import pygtk,gtk
from os import popen
from re import findall
from urllib2 import urlopen

class Main_Window:
    def __init__(self):
        self.Get_Fglist()
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy",self.destroy)
        self.window.connect("delete_event",self.delete_event)
        VBox = gtk.VBox(False , 1)
        for tuple in self.list:
            VBox.pack_start(self.create_line(tuple), False , False , 2)
        Btn_Reload = gtk.Button("Reload")
        Btn_Reload.connect("clicked", self.Btn_Reload_callback)
        VBox.pack_end(Btn_Reload, False, False, 10)
        self.window.add(VBox)
        self.window.show_all()
    
    def create_line(self,tuple):
        Lbl_hour = gtk.Label(tuple[0])
        Lbl_artist = gtk.Label(tuple[2])
        Lbl_title = gtk.Label(tuple[1])
        HBox = gtk.HBox(False , 5)
        HBox.pack_start(Lbl_hour, False, False, 5)
        HBox.pack_start(Lbl_artist, False, False, 5)
        HBox.pack_start(Lbl_title, False, False, 5)
        return HBox
    
    def Get_Fglist(self):
        try:
            fg = urlopen("http://www.radiofg.com/modules.php?name=Fg_Titres").read()
        except:
            print 'can\'t download http://www.radiofg.com/modules.php?name=Fg_Titres'
        REGEX = "<td class=\"textetrack\">\s+<div align=\"center\">\s+([\d+:]+).*</div></td>\s+<td class=\"textetrack\">(.*)</td>\s+<td class=\"textetrack\">(.*)</td>"
        self.list = findall(REGEX,fg)    
    
    def Btn_Reload_callback(self , widget):
        print "reload"
        self.Get_Fglist()
    
    def delete_event(self, widget, event):
       """delete_event is call when the close button is pressed"""
       return False #False call the destroy signal True don't
    
    def destroy(self, widget):
       """called by the delete_event return"""
       gtk.main_quit()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    mainwindow = Main_Window()
    mainwindow.main()
    